home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gp_mswin.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  19KB  |  703 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gp_mswin.c */
  20. /*
  21.  * Microsoft Windows 3.n platform support for Ghostscript.
  22.  * Original version by Russell Lang and Maurice Castro with help from
  23.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  24.  * initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
  25.  */
  26.  
  27. #include "windows_.h"
  28. #if WINVER >= 0x030a
  29. #include <shellapi.h>
  30. #endif
  31. #include "stdio_.h"
  32. #include <stdlib.h>
  33. #include <stdarg.h>
  34. #include "ctype_.h"
  35. #include "dos_.h"
  36. #include <io.h>
  37. #include "malloc_.h"
  38. #include "gp_mswin.h"
  39. #include "gp_mswtx.h"
  40.  
  41. #include "memory_.h"
  42. #include "gx.h"
  43. #include "gp.h"
  44. #include "gpcheck.h"
  45. #include "gserrors.h"
  46. #include "gxdevice.h"
  47.  
  48. #include <fcntl.h>
  49. #include <signal.h>
  50. #include "string_.h"
  51.  
  52. /* for imitation pipes */
  53. #include "stream.h"
  54. #include "filedev.h"            /* must come after stream.h */
  55. extern stream *gs_stream_stdin;        /* from zfiledev.c */
  56. extern stream *gs_stream_stdout;    /* from zfiledev.c */
  57. extern stream *gs_stream_stderr;    /* from zfiledev.c */
  58.  
  59. /* Library routines not declared in a standard header */
  60. extern char *getenv(P1(const char *));
  61.  
  62. /* Imported from gsmain.c */
  63. extern void gs_exit(P1(int exit_status));
  64.  
  65. /* Imported from gp_msdos.c */
  66. int gp_file_is_console(P1(FILE *));
  67.  
  68. /* ------ from gnuplot winmain.c plus new stuff ------ */
  69.  
  70. /* limits */
  71. #define MAXSTR 255
  72.  
  73. /* public handles */
  74. HWND hwndtext;
  75. HINSTANCE phInstance;
  76.  
  77. const LPSTR szAppName = "Ghostscript";
  78. const LPSTR szImgName = "Ghostscript Image";
  79. char FAR win_prntmp[MAXSTR];    /* filename of PRN temporary file */
  80. int win_init = 0;        /* flag to know if gp_exit has been called */
  81. int win_exit_status;
  82. BOOL is_win31 = FALSE;
  83.  
  84. /* gsview.exe */
  85. BOOL gsview = FALSE;
  86. HWND gsview_hwnd = NULL;
  87. BOOL gsview_next = FALSE;
  88. LPSTR gsview_option = "-sGSVIEW=";
  89.  
  90. /* redirected stdio */
  91. TW textwin;    /* text window structure */
  92.  
  93. /* imitation pipes */
  94. HGLOBAL pipe_hglobal = NULL;
  95. LPBYTE pipe_lpbyte = NULL;
  96. UINT pipe_count = 0;
  97.  
  98. BOOL CALLBACK _export AbortProc(HDC, int);
  99. int main(int argc, char *argv[], char *env[]);
  100.  
  101. /* our exit handler */
  102. /* also called from Text Window WM_CLOSE */
  103. void win_exit(void)
  104. {
  105. #if WINVER >= 0x030a
  106.     /* disable Drag Drop */
  107.     if (is_win31)
  108.         DragAcceptFiles(hwndtext, FALSE);
  109. #endif
  110.     /* if we didn't exit through gs_exit() then do so now */
  111.     if (win_init)
  112.         gs_exit(0);
  113.  
  114.     fcloseall();
  115.     if (win_exit_status) {
  116.         /* display message box so error messages in hwndtext can be read */
  117.         char buf[20];
  118.         sprintf(buf, "Exit code %d",win_exit_status);
  119.         MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  120.     }
  121.     
  122.     /* tell gsview that we are closing */
  123.     if (gsview)
  124.         SendMessage(gsview_hwnd, WM_GSVIEW, GSWIN_CLOSE, (LPARAM)NULL);
  125.  
  126.     TextClose(&textwin);
  127. }
  128.  
  129. int PASCAL 
  130. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
  131. {
  132.     int i;
  133. #if defined(_MSC_VER)    /* MSC doesn't give us _argc and _argv[] so ...   */
  134. #define MAXCMDTOKENS 128
  135.     int     _argc=0;
  136.     LPSTR   _argv[MAXCMDTOKENS];
  137.     _argv[_argc] = "gswin.exe";
  138.     _argv[++_argc] = _fstrtok( lpszCmdLine, " ");
  139.     while (_argv[_argc] != NULL)
  140.         _argv[++_argc] = _fstrtok( NULL, " ");
  141. #endif
  142.     is_win31 = FALSE;
  143.     {
  144.     WORD version = LOWORD(GetVersion());
  145.     if (((LOBYTE(version)<<8) | HIBYTE(version)) >= 0x30a)
  146.         is_win31 = TRUE;
  147.     }
  148.  
  149.     if (hPrevInstance) {
  150.         MessageBox((HWND)NULL,"Can't run twice", szAppName, MB_ICONHAND | MB_OK);
  151.         return FALSE;
  152.     }
  153.  
  154.     /* copy the hInstance into a variable so it can be used */
  155.     phInstance = hInstance;
  156.  
  157.         /* start up the text window */
  158.     textwin.hInstance = hInstance;
  159.     textwin.hPrevInstance = hPrevInstance;
  160.     textwin.nCmdShow = cmdShow;
  161.     textwin.Title = szAppName;
  162.     textwin.hIcon = LoadIcon(hInstance, "texticon");
  163.     textwin.DragPre = "(";
  164.     textwin.DragPost = ") run\r";
  165.     textwin.ScreenSize.x = 80;
  166.     textwin.ScreenSize.y = 80;
  167.     textwin.KeyBufSize = 2048;
  168.     textwin.CursorFlag = 1;    /* scroll to cursor after \n & \r */
  169.     textwin.shutdown = win_exit;
  170.  
  171.     if (TextInit(&textwin))
  172.         exit(1);
  173.     hwndtext = textwin.hWndText;
  174.  
  175.     (void) atexit((atexit_t)win_exit); /* setup exit handler */
  176.  
  177.     /* check if we are to use gsview.exe */
  178.     for (i=0; i<_argc; i++) {
  179.         if (!strncmp(_argv[i], gsview_option, strlen(gsview_option))) {
  180.         gsview_hwnd = (HWND)atoi(_argv[i]+strlen(gsview_option));
  181.         if (gsview_hwnd != (HWND)NULL) {
  182.             if (!IsWindow(gsview_hwnd)) {
  183.                 char buf[80];
  184.                 sprintf(buf,"GSVIEW window handle %u is invalid",(int)gsview_hwnd);
  185.                 MessageBox(hwndtext, buf, szAppName, MB_OK);
  186.                 return 0;
  187.             }
  188.             gsview = TRUE;
  189.             /* give gsview the handle to our text window */
  190.             SendMessage(gsview_hwnd, WM_GSVIEW, HWND_TEXT, (LPARAM)hwndtext);
  191.         }
  192.         }
  193.     }
  194.  
  195.     main(_argc, _argv, environ);
  196.  
  197.     /* never reached */
  198.     win_exit(); 
  199.     return 0;
  200. }
  201.  
  202.  
  203. BOOL CALLBACK _export
  204. AbortProc(HDC hdcPrn, int code)
  205. {
  206.     (void)gp_check_interrupts();
  207.     return(TRUE);
  208. }
  209.   
  210. /* ------ Process message loop ------ */
  211. /*
  212.  * Check messages and interrupts; return true if interrupted.
  213.  * This is called frequently - it must be quick!
  214.  */
  215. int
  216. gp_check_interrupts(void)
  217. {
  218.     TextMessage();
  219.     return 0;
  220. }
  221.  
  222. /* ====== Generic platform procedures ====== */
  223.  
  224. /* ------ Initialization/termination (from gp_itbc.c) ------ */
  225.  
  226. /* Do platform-dependent initialization. */
  227. void
  228. gp_init(void)
  229. {
  230.     win_init = 1;
  231. }
  232.  
  233. /* Do platform-dependent cleanup. */
  234. void
  235. gp_exit(int exit_status, int code)
  236. {
  237.     win_init = 0;
  238.     win_exit_status = exit_status;
  239. }
  240.  
  241. /* ------ Printer accessing ------ */
  242.   
  243. /* Forward references */
  244. private int gp_printfile(P1(char *));
  245.  
  246. /* Open a connection to a printer.  A null file name means use the */
  247. /* standard printer connected to the machine, if any. */
  248. /* Return NULL if the connection could not be opened. */
  249. FILE *
  250. gp_open_printer(char *fname, int binary_mode)
  251. {    if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
  252.     {    FILE *pfile;
  253.         pfile = gp_open_scratch_file(gp_scratch_file_name_prefix, 
  254.             win_prntmp, "wb");
  255.         return pfile;
  256.     }
  257.     else
  258.         return fopen(fname, (binary_mode ? "wb" : "w"));
  259. }
  260.  
  261. /* Close the connection to the printer. */
  262. void
  263. gp_close_printer(FILE *pfile, const char *fname)
  264. {
  265.     fclose(pfile);
  266.     if (strlen(fname) && strcmp(fname,"PRN"))
  267.         return;        /* a file, not a printer */
  268.  
  269.     gp_printfile(win_prntmp);
  270.     unlink(win_prntmp);
  271. }
  272.  
  273. /* Windows does not provide API's in the SDK for writing directly to a */
  274. /* printer.  Instead you are supposed to use the Windows printer drivers. */
  275. /* Ghostscript has its own printer drivers, so we need to use some API's */
  276. /* that are documented only in the Device Driver Adaptation Guide */
  277. /* that comes with the DDK.  Prototypes taken from DDK <print.h> */
  278. DECLARE_HANDLE(HPJOB);
  279.  
  280. HPJOB   WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
  281. int     WINAPI StartSpoolPage(HPJOB);
  282. int     WINAPI EndSpoolPage(HPJOB);
  283. int     WINAPI WriteSpool(HPJOB, LPSTR, int);
  284. int     WINAPI CloseJob(HPJOB);
  285. int     WINAPI DeleteJob(HPJOB, int);
  286. int     WINAPI WriteDialog(HPJOB, LPSTR, int);
  287. int     WINAPI DeleteSpoolPage(HPJOB);
  288.  
  289. HWND hDlgModeless;
  290.  
  291. /* Modeless dialog box - Cancel printing */
  292. BOOL CALLBACK _export
  293. CancelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  294. {
  295.     switch(message) {
  296.     case WM_INITDIALOG:
  297.         SetWindowText(hDlg, szAppName);
  298.         return TRUE;
  299.     case WM_COMMAND:
  300.         switch(LOWORD(wParam)) {
  301.         case IDCANCEL:
  302.             DestroyWindow(hDlg);
  303.             hDlgModeless = 0;
  304.             EndDialog(hDlg, 0);
  305.             return TRUE;
  306.         }
  307.     }
  308.     return FALSE;
  309. }
  310.  
  311. /* Dialog box to select printer port */
  312. BOOL CALLBACK _export
  313. SpoolDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  314. {
  315. LPSTR entry;
  316.     switch(message) {
  317.     case WM_INITDIALOG:
  318.         entry = (LPSTR)lParam;
  319.         while (*entry) {
  320.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_ADDSTRING, 0, (LPARAM)entry);
  321.         entry += lstrlen(entry)+1;
  322.         }
  323.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_SETCURSEL, 0, (LPARAM)0);
  324.         return TRUE;
  325.     case WM_COMMAND:
  326.         switch(LOWORD(wParam)) {
  327.         case SPOOL_PORT:
  328. #ifdef WIN32
  329.             if (HIWORD(wParam)
  330. #else
  331.             if (HIWORD(lParam)
  332. #endif
  333.                            == LBN_DBLCLK)
  334.             PostMessage(hDlg, WM_COMMAND, IDOK, 0L);
  335.             return FALSE;
  336.         case IDOK:
  337.             EndDialog(hDlg, 1+(int)SendDlgItemMessage(hDlg, SPOOL_PORT, LB_GETCURSEL, 0, 0L));
  338.             return TRUE;
  339.         case IDCANCEL:
  340.             EndDialog(hDlg, 0);
  341.             return TRUE;
  342.         }
  343.     }
  344.     return FALSE;
  345. }
  346.  
  347. /* Print File to port */
  348. private int
  349. gp_printfile(char *filename)
  350. {
  351. #define PRINT_BUF_SIZE 16384u
  352. char *buffer;
  353. char *portname;
  354. DLGPROC lpfnSpoolProc;
  355. int i, port;
  356. HPJOB hJob;
  357. WORD count;
  358. FILE *f;
  359. int error = FALSE;
  360. DLGPROC lpfnCancelProc;
  361. long lsize;
  362. long ldone;
  363. char pcdone[20];
  364. MSG msg;
  365.  
  366.     /* get list of ports */
  367.     if ((buffer = malloc(PRINT_BUF_SIZE)) == (char *)NULL)
  368.         return FALSE;
  369.     GetProfileString("ports", NULL, "", buffer, PRINT_BUF_SIZE);
  370.     /* select a port */
  371.     lpfnSpoolProc = (DLGPROC)MakeProcInstance((FARPROC)SpoolDlgProc, phInstance);
  372.     port = DialogBoxParam(phInstance, "SpoolDlgBox", hwndtext, lpfnSpoolProc, (LPARAM)buffer);
  373.     FreeProcInstance((FARPROC)lpfnSpoolProc);
  374.     if (!port) {
  375.         free(buffer);
  376.         return FALSE;
  377.     }
  378.     portname = buffer;
  379.     for (i=1; i<port && strlen(portname)!=0; i++)
  380.         portname += lstrlen(portname)+1;
  381.     
  382.     if ((f = fopen(filename, "rb")) == (FILE *)NULL) {
  383.         free(buffer);
  384.         return FALSE;
  385.     }
  386.     fseek(f, 0L, SEEK_END);
  387.     lsize = ftell(f);
  388.     if (lsize <= 0)
  389.         lsize = 1;
  390.     fseek(f, 0L, SEEK_SET);
  391.  
  392.     hJob = OpenJob(portname, filename, (HDC)NULL);
  393.     switch ((int)hJob) {
  394.         case SP_APPABORT:
  395.         case SP_ERROR:
  396.         case SP_OUTOFDISK:
  397.         case SP_OUTOFMEMORY:
  398.         case SP_USERABORT:
  399.             fclose(f);
  400.         free(buffer);
  401.             return FALSE;
  402.     }
  403.     if (StartSpoolPage(hJob) < 0)
  404.         error = TRUE;
  405.  
  406.     lpfnCancelProc = (DLGPROC)MakeProcInstance((FARPROC)CancelDlgProc, phInstance);
  407.     hDlgModeless = CreateDialog(phInstance, "CancelDlgBox", hwndtext, lpfnCancelProc);
  408.     ldone = 0;
  409.  
  410.     while (!error && hDlgModeless 
  411.       && (count = fread(buffer, 1, PRINT_BUF_SIZE, f)) != 0 ) {
  412.         if (WriteSpool(hJob, buffer, count) < 0)
  413.         error = TRUE;
  414.         ldone += count;
  415.         sprintf(pcdone, "%d %%done", (int)(ldone * 100 / lsize));
  416.         SetWindowText(GetDlgItem(hDlgModeless, CANCEL_PCDONE), pcdone);
  417.         while (PeekMessage(&msg, hDlgModeless, 0, 0, PM_REMOVE)) {
  418.             if ((hDlgModeless == 0) || !IsDialogMessage(hDlgModeless, &msg)) {
  419.             TranslateMessage(&msg);
  420.             DispatchMessage(&msg);
  421.           }
  422.           }
  423.       }
  424.     free(buffer);
  425.     fclose(f);
  426.  
  427.     if (!hDlgModeless)
  428.         error=TRUE;
  429.     DestroyWindow(hDlgModeless);
  430.     hDlgModeless = 0;
  431.     FreeProcInstance((FARPROC)lpfnCancelProc);
  432.     EndSpoolPage(hJob);
  433.     if (error)
  434.         DeleteJob(hJob, 0);
  435.     else
  436.         CloseJob(hJob);
  437.     return !error;
  438. }
  439.  
  440. /* ------ File names ------ */
  441.  
  442. /* Create and open a scratch file with a given name prefix. */
  443. /* Write the actual file name at fname. */
  444. FILE *
  445. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  446. {    char *temp;
  447.     if ( (temp = getenv("TEMP")) == NULL )
  448.         *fname = 0;
  449.     else
  450.     {    strcpy(fname, temp);
  451.         /* Prevent X's in path from being converted by mktemp. */
  452.         for ( temp = fname; *temp; temp++ )
  453.             *temp = tolower(*temp);
  454.         if ( strlen(fname) && (fname[strlen(fname)-1] != '\\') )
  455.             strcat(fname, "\\");
  456.     }
  457.     strcat(fname, prefix);
  458.     strcat(fname, "XXXXXX");
  459.     mktemp(fname);
  460.     return fopen(fname, mode);
  461. }
  462.  
  463. /* ------ File operations ------ */
  464.  
  465. /* If the file given by fname exists, fill in its status and return 1; */
  466. /* otherwise return 0. */
  467. int
  468. gp_file_status(const char *fname, file_status *pstatus)
  469. {    FILE *f = fopen(fname, "r");
  470.     long flen;
  471.     struct ftime ft;
  472.     if ( f == NULL ) return 0;
  473.     if ( getftime(fileno(f), &ft) < 0 )
  474.        {    fclose(f);
  475.         return 0;
  476.        }
  477.     fseek(f, 0, SEEK_END);
  478.     flen = ftell(f);
  479.     pstatus->size_pages = (flen + 1023) >> 10;
  480.     pstatus->size_bytes = flen;
  481.     /* Make a single long value from the ftime structure. */
  482.     pstatus->time_referenced = pstatus->time_created =
  483.       ((long)((ft.ft_year << 9) + (ft.ft_month << 5) + ft.ft_day) << 16) +
  484.       ((ft.ft_hour << 11) + (ft.ft_min << 5) + ft.ft_tsec);
  485.     fclose(f);
  486.     return 1;
  487. }
  488.  
  489. /* ====== Substitute for stdio ====== */
  490.  
  491. /* Forward references */
  492. private void win_std_init(void);
  493. private void win_pipe_init(void);
  494. private int win_std_read_buf(P1(stream *s));
  495. private int win_std_write_buf(P1(stream *s));
  496.  
  497. /* Use a pseudo file device to get win_stdio_init called at the right time. */
  498. /* This is bad architecture; we'll fix it later. */
  499. private fdev_proc_init(win_stdio_init);
  500. const file_device gs_fdev_wstdio = {
  501.     "wstdio",
  502.     { win_stdio_init, fdev_no_open_device,
  503.       fdev_no_open_file, fdev_no_fopen, fdev_no_fclose,
  504.       fdev_no_delete_file, fdev_no_rename_file,
  505.       fdev_no_enumerate_files
  506.     }
  507. };
  508.  
  509. /* Do one-time initialization */
  510. private int
  511. win_stdio_init(file_device *fdev)
  512. {
  513.     win_std_init();        /* redefine stdin/out/err to our window routines */
  514.     if (gsview)
  515.         win_pipe_init();    /* redefine stdin to be a pipe */
  516.     return 0;
  517. }
  518.  
  519. /* reinitialize stdin/out/err to use our windows */
  520. /* assume stream has already been initialized for the real stdin */
  521. private void
  522. win_std_init(void)
  523. {
  524.     if ( gp_file_is_console(gs_stream_stdin->file) )
  525.     {    /* Allocate a real buffer for stdin. */
  526.         /* The size must not exceed the size of the */
  527.         /* lineedit buffer.  (This is a hack.) */
  528. #define win_stdin_buf_size 160
  529.         static const stream_procs pin =
  530.         {    s_std_noavailable, s_std_noseek,
  531.             s_std_read_flush, s_std_close,
  532.             win_std_read_buf, NULL
  533.         };
  534.         byte *buf = (byte *)gs_malloc(win_stdin_buf_size, 1,
  535.                           "win_stdin_init");
  536.         s_std_init(gs_stream_stdin, buf, win_stdin_buf_size,
  537.                &pin, s_mode_read);
  538.         gs_stream_stdin->file = NULL;
  539.     }
  540.  
  541.     {    static const stream_procs pout =
  542.         {    s_std_noavailable, s_std_noseek,
  543.             s_std_write_flush, s_std_close,
  544.             NULL, win_std_write_buf 
  545.         };
  546.         if ( gp_file_is_console(gs_stream_stdout->file) )
  547.         {    gs_stream_stdout->procs = pout;
  548.             gs_stream_stdout->file = NULL;
  549.         }
  550.         if ( gp_file_is_console(gs_stream_stderr->file) )
  551.         {    gs_stream_stderr->procs = pout;
  552.             gs_stream_stderr->file = NULL;
  553.         }
  554.     }
  555.  
  556. }
  557.  
  558. private int
  559. win_std_read_buf(register stream *s)
  560. {
  561.     byte *buf = s->cbuf;
  562.     /* Implement line editing here. */
  563. #define ERASELINE 21        /* ^U */
  564. #define ERASECHAR1 8        /* ^H */
  565. #define ERASECHAR2 127        /* DEL */
  566.     byte *dest = buf - 1;
  567.     byte *limit = dest + s->bsize - 1;  /* always leave room for \n */
  568.     uint ch;
  569.     while ( (ch = TextGetCh(&textwin)) != '\n' )
  570.     {    switch ( ch )
  571.         {
  572.         default:
  573.             if ( dest == limit )
  574.                 MessageBeep(-1);
  575.             else
  576.             {    *++dest = ch;
  577.                 TextPutCh(&textwin, ch);
  578.             }
  579.             break;
  580.         case ERASELINE:
  581.             while ( dest >= buf )
  582.             {    TextPutCh(&textwin, '\b');
  583.                 TextPutCh(&textwin, ' ');
  584.                 TextPutCh(&textwin, '\b');
  585.                 dest--;
  586.             }
  587.             break;
  588.         case ERASECHAR1:
  589.         case ERASECHAR2:
  590.             if ( dest >= buf )
  591.             {    TextPutCh(&textwin, '\b');
  592.                 TextPutCh(&textwin, ' ');
  593.                 TextPutCh(&textwin, '\b');
  594.                 dest--;
  595.             }
  596.             break;
  597.         }
  598.     }
  599.     *++dest = ch;
  600.     TextPutCh(&textwin, ch);
  601.     s->cptr = buf - 1;
  602.     s->end_status = 0;
  603.     s->endptr = dest;
  604.     return 0;
  605. }
  606.  
  607. private int
  608. win_std_write_buf(register stream *s)
  609. {    uint count = s->cptr + 1 - s->cbuf;
  610.     TextWriteBuf(&textwin, s->cbuf, count);
  611.     s->cptr = s->cbuf - 1;
  612.     s->endptr = s->cptr + s->bsize;
  613.     gp_check_interrupts();
  614.     return 0;
  615. }
  616.  
  617. /* This is used instead of the stdio version. */
  618. /* The declaration must be identical to that in <stdio.h>. */
  619. int _Cdecl _FARFUNC
  620. fprintf(FILE _FAR *file, const char *fmt, ...)
  621. {
  622. int count;
  623. va_list args;
  624.     va_start(args,fmt);
  625.     if ( gp_file_is_console(file) ) {
  626.         char buf[1024];
  627.         count = vsprintf(buf,fmt,args);
  628. /*        MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP); */
  629.         TextWriteBuf(&textwin, buf, count);
  630.     }
  631.     else
  632.         count = vfprintf(file, fmt, args);
  633.     va_end(args);
  634.     return count;
  635. }
  636.  
  637. /* ------ Imitation pipes (only used with gsview) ------ */
  638.  
  639. /* Forward references */
  640. private int win_pipe_read_buf(P1(stream *));
  641. private void win_pipe_request(void);
  642. private int win_pipe_read(char *, unsigned int);
  643.  
  644. /* reinitialize stdin stream to read from imitation pipe */
  645. /* assume stream has already been initialized for the windows stdin */
  646. private void
  647. win_pipe_init(void)
  648. {    if ( gs_stream_stdin->procs.read_buf == win_std_read_buf )
  649.     {    static const stream_procs p =
  650.         {    s_std_noavailable, s_std_noseek,
  651.             s_std_read_flush, s_std_close,
  652.             win_pipe_read_buf, NULL
  653.         };
  654.         gs_stream_stdin->procs = p;
  655.     }
  656. }
  657.  
  658. private int
  659. win_pipe_read_buf(register stream *s)
  660. {    int nread;
  661.     nread = win_pipe_read(s->cbuf, 1);
  662.     s->cptr = s->cbuf - 1;
  663.     s->end_status = (nread <= 0 ? EOFC : 0);
  664.     if ( nread <= 0 ) nread = 0;
  665.     s->endptr = s->cptr + nread;
  666.     return 0;
  667. }
  668.  
  669. /* get a block from gsview */
  670. private void
  671. win_pipe_request(void)
  672. {
  673.     /* request another block */
  674.     if (pipe_lpbyte != (LPBYTE)NULL)
  675.             GlobalUnlock(pipe_hglobal);
  676.     if (pipe_hglobal != (HGLOBAL)NULL)
  677.             GlobalFree(pipe_hglobal);
  678.     pipe_hglobal = (HGLOBAL)NULL;
  679.     pipe_lpbyte = (LPBYTE)NULL;
  680.  
  681.     SendMessage(gsview_hwnd, WM_GSVIEW, PIPE_REQUEST, 0L);
  682.     /* wait for block */
  683.     while (pipe_hglobal == (HGLOBAL)NULL)
  684.         gp_check_interrupts();
  685.     pipe_lpbyte = GlobalLock(pipe_hglobal);
  686. }
  687.  
  688. /* pipe_read - similar to fread */
  689. private int
  690. win_pipe_read(char *ptr, unsigned int count)
  691. {
  692.     int n;
  693.     if (!pipe_count)
  694.         win_pipe_request();
  695.     if (!pipe_count)
  696.         return -1;
  697.     n = min(count, pipe_count);
  698.     memcpy(ptr, pipe_lpbyte, n);
  699.     pipe_lpbyte += n;
  700.     pipe_count -= n;
  701.     return n;
  702. }
  703.